package com.block.product.service.union;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.vo.ResponseVo;
import com.block.db.common.vo.BaseUserInfoVo;
import com.block.db.entity.ProductInfo;
import com.block.product.service.IUnionProductService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class DefaultProductServiceImpl implements IUnionProductService {


    @Override
    public ResponseVo matchUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {
        return new ResponseVo(AppRspCodeConstant.SUCCESS);
    }

    @Override
    public ResponseVo registerUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("productUrl", "https://www.baidu.com");
        return new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
    }

    public static void main(String[] args) {

        String requestUrl = "https://4b3vdz2hdjho7gzuo4wl2jgsoe.appsync-api.ap-southeast-1.amazonaws.com/graphql";

        String queryData = "query GetStatistics{ getStatistics(ammAddr: \"0x8c835dfaa34e2ae61775e80ee29e2c724c6ae2bb\") { volume24h priceChangeRate24h priceHigh24h priceLow24h }}";

//        String queryData = "query GetStatistics($ammAddress: String!){" +
//                " getStatistics(ammAddr: $ammAddress) {" +
//                " volume24h" +
//                " priceChangeRate24h" +
//                " priceHigh24h" +
//                " priceLow24h" +
//                " }" +
//                "}" +
//                "";

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("x-api-key", "da2-lq3fkypifbfp3mzfdkac26avzm");
        headerMap.put("Content-Type", "application/json");

        JSONObject variables = new JSONObject();
        variables.put("ammAddress", "0x34235C8489b06482A99bb7fcaB6d7c467b92d248");

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("query", queryData);
//        paramMap.put("variables", variables);
//        paramMap.put("operationName", "GetStatistics");

        System.out.println(requestUrl);
        System.out.println(JSON.toJSONString(headerMap));
        System.out.println(JSON.toJSONString(paramMap));

        HttpRequest httpRequest = HttpRequest.post(requestUrl);
        httpRequest.headerMap(headerMap, true);
        httpRequest.body(JSON.toJSONString(paramMap));
        String result = httpRequest.execute().body();

        System.out.println(result);

    }
}
